import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/components/layout/Layout.vue'
import AdminLayout from '@/components/layout/AdminLayout.vue'
import TestAPI from '@/TestAPI.vue'

// 路由配置
const routes = [
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '',
        name: 'Home',
        component: () => import('@/views/Home.vue'),
        meta: { title: '首页', requiresAuth: false }
      },
      {
        path: 'login',
        name: 'Login',
        component: () => import('@/views/Login.vue'),
        meta: { title: '登录', requiresAuth: false }
      },
      {
        path: 'register',
        name: 'Register',
        component: () => import('@/views/Register.vue'),
        meta: { title: '注册', requiresAuth: false }
      },
      {
        path: 'board/:id',
        name: 'BoardDetail',
        component: () => import('@/views/ArticleList.vue'),
        meta: { title: '板块详情', requiresAuth: false }
      },
      {
        path: 'articles',
        name: 'Articles',
        component: () => import('@/views/ArticleList.vue'),
        meta: { title: '文章列表', requiresAuth: false }
      },
      {
        path: 'article/create',
        name: 'ArticleCreate',
        component: () => import('@/views/ArticleCreate.vue'),
        meta: { title: '发布文章', requiresAuth: true }
      },
      {
        path: 'create-article',
        name: 'CreateArticle',
        component: () => import('@/views/CreateArticle.vue'),
        meta: { title: '发布文章', requiresAuth: true }
      },
      {
        path: 'article/:id',
        name: 'ArticleDetail',
        component: () => import('@/views/ArticleDetail.vue'),
        meta: { title: '文章详情', requiresAuth: false }
      },
      {
        path: 'article/:id/edit',
        name: 'ArticleEdit',
        component: () => import('@/views/ArticleEdit.vue'),
        meta: { title: '编辑文章', requiresAuth: true }
      },
      {
        path: 'user/:id(\\d+)',
        name: 'UserProfile',
        component: () => import('@/views/UserProfile.vue'),
        meta: { title: '用户中心', requiresAuth: false }
      },
      {
        path: 'user/profile',
        name: 'CurrentUserProfile',
        component: () => import('@/views/UserProfile.vue'),
        meta: { title: '我的帖子', requiresAuth: true }
      },
      {
        path: 'settings',
        name: 'UserSettings',
        component: () => import('@/views/UserSettings.vue'),
        meta: { title: '个人设置', requiresAuth: true }
      },
      {
        path: 'messages',
        name: 'Messages',
        component: () => import('@/views/Messages.vue'),
        meta: { title: '消息中心', requiresAuth: true }
      },
      {
        path: 'drafts',
        name: 'Drafts',
        component: () => import('@/views/CreateArticle.vue'),
        meta: { title: '我的草稿', requiresAuth: true }
      },
      {
        path: 'announcements',
        name: 'Announcements',
        component: () => import('../views/Announcements.vue'),
        meta: { title: '系统公告' }
      }
    ]
  },
  // 热门文章路由
  {
    path: '/hot',
    component: Layout,
    children: [
      {
        path: '',
        name: 'HotArticles',
        component: () => import('@/views/ArticleList.vue'),
        meta: { title: '热门文章', requiresAuth: false }
      }
    ]
  },
  // trending路径作为hot的别名
  {
    path: '/trending',
    component: Layout,
    children: [
      {
        path: '',
        name: 'TrendingArticles',
        component: () => import('@/views/ArticleList.vue'),
        meta: { title: '热门文章', requiresAuth: false }
      }
    ]
  },
  // 最新文章路由
  {
    path: '/new',
    component: Layout,
    children: [
      {
        path: '',
        name: 'NewArticles',
        component: () => import('@/views/ArticleList.vue'),
        meta: { title: '最新文章', requiresAuth: false }
      }
    ]
  },
  // newest路径作为new的别名
  {
    path: '/newest',
    component: Layout,
    children: [
      {
        path: '',
        name: 'NewestArticles',
        component: () => import('@/views/ArticleList.vue'),
        meta: { title: '最新文章', requiresAuth: false }
      }
    ]
  },
  // 搜索页面路由
  {
    path: '/search',
    component: Layout,
    children: [
      {
        path: '',
        name: 'Search',
        component: () => import('@/views/Search.vue'),
        meta: { title: '搜索结果', requiresAuth: false }
      }
    ]
  },
  // Add test API route
  {
    path: '/test-api',
    name: 'TestAPI',
    component: TestAPI,
    meta: { title: 'API测试', requiresAuth: false }
  },
  // 管理后台路由
  {
    path: '/admin',
    component: AdminLayout,
    meta: { requiresAuth: true, requiresAdmin: true },
    children: [
      {
        path: '',
        name: 'AdminDashboard',
        component: () => import('@/views/admin/Dashboard.vue'),
        meta: { title: '控制台', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'users',
        name: 'AdminUsers',
        component: () => import('@/views/admin/Users.vue'),
        meta: { title: '用户管理', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'articles',
        name: 'AdminArticles',
        component: () => import('@/views/admin/Articles.vue'),
        meta: { title: '文章管理', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'comments',
        name: 'AdminComments',
        component: () => import('@/views/admin/Comments.vue'),
        meta: { title: '评论管理', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'boards',
        name: 'AdminBoards',
        component: () => import('@/views/admin/Boards.vue'),
        meta: { title: '板块管理', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'settings',
        name: 'AdminSettings',
        component: () => import('@/views/admin/Settings.vue'),
        meta: { title: '系统设置', requiresAuth: true, requiresAdmin: true }
      }
    ]
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue'),
    meta: { title: '页面不存在', requiresAuth: false }
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 论坛系统` : '论坛系统'
  
  // 从本地存储中获取用户登录状态和角色
  const isAuthenticated = !!localStorage.getItem('token')
  const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
  const isAdmin = userInfo?.isAdmin === 1  // 修改这里，使用isAdmin属性判断
  
  // 检查是否需要登录权限
  if (to.meta.requiresAuth && !isAuthenticated) {
    next({ name: 'Login', query: { redirect: to.fullPath } })
  } 
  // 检查是否需要管理员权限
  else if (to.meta.requiresAdmin && !isAdmin) {
    next({ name: 'Home' })
  } 
  else {
    next()
  }
})

export default router 